package com.zut.hospital.controller.diagnose;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.dao.staff.OfficeMapper;
import com.zut.hospital.entity.diagnose.Patient;
import com.zut.hospital.service.diagnose.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author 侯捷
 * @package_name com.zut.hostest.controller
 * @date 2019/5/17 16:56
 * @description
 */
@Controller
@RequestMapping("/hospital/patient")
public class PatientController {

    @Autowired
    PatientService patientService;

    @Autowired
    OfficeMapper officeMapper;


    @ResponseBody
    @RequestMapping("register/get")
    public List<Patient> getAllPatient(){
        return patientService.getAllPatient();
    }
    @ResponseBody
    @RequestMapping(value = "register/insert",method = RequestMethod.POST)
    public int insertOnePatient(@RequestBody String message){
        Patient p = JSONObject.parseObject(message,Patient.class);
//        System.out.println(patientService.getTimeToSql());
//        String s=p.getPatRemark().substring(0,4)+p.getPatRemark().substring(5,7)+p.getPatRemark().substring(p.getPatRemark().length()-2,p.getPatRemark().length());
        p.setPatConven("A" + p.getPatOfcId()+ "--" +patientService.getTime()+ p.getPatOfcId());
        //创建信息的时间
        p.setPatCreateTime(patientService.getTimeToSql());
        System.out.println(p.getPatName());
        System.out.println(p.getPatRemark());//预约挂号的时间
        System.out.println("*********:"+p.getPatCreateTime());
        return patientService.insertOnePatient(p);
    }
    @ResponseBody
    @RequestMapping(value = "register/select" ,method = RequestMethod.POST)
    public List<Patient> selectOnePatientByStatus(@RequestBody String status){
        Patient patient=JSONObject.parseObject(status,Patient.class);
        return patientService.selectOnePatientByStatus(patient.getPatStatus());
    }
    @ResponseBody
    @RequestMapping(value = "register/update",method = RequestMethod.POST)
    public int updateStatus(@RequestBody String data){
        Patient patient=JSONObject.parseObject(data,Patient.class);
        return patientService.updateStatus(patient,patient.getId());
    }
    @ResponseBody
    @RequestMapping(value = "register/select/status",method = RequestMethod.POST)
    public long countByStatus(@RequestBody String data){
        Patient patient=JSONObject.parseObject(data,Patient.class);
        return patientService.countByStatus(patient.getPatStatus());
    }
    @ResponseBody
    @RequestMapping(value = "register/select/between/time")
    public  void countBetweenTime(@RequestBody String data){
        Calendar calendar = new GregorianCalendar();
        Calendar calendar1 = new GregorianCalendar();
//        Patient patient=JSONObject.parseObject(data,Patient.class);
//        return patientService.countBetweenTime(patient.getPatCreateTime(),patient.getPatCreateTime());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        JSONObject jsonObject=JSON.parseObject(data);
        String patCreatTime1=jsonObject.getString("patCreatTime1");
        String patCreatTime2=jsonObject.getString("patCreatTime2");
        try {
            Date time1 = simpleDateFormat.parse(patCreatTime1);
            Date time2=simpleDateFormat.parse(patCreatTime2);
            calendar.setTime(time2);
            calendar1.setTime(time1);
            calendar.add(calendar.DATE,1);
            calendar1.add(calendar1.DATE,1);
           // System.out.println(time2+"****2");
           // System.out.println(calendar.getTime()+"****4");
           // System.out.println(calendar1.getTime()+"*****5");
           // System.out.println(patientService.countBetweenTime(calendar1.getTime(),calendar.getTime())+"****3");
        }
        catch (ParseException e){
            System.out.println("error!!!");
        }
    }

    @ResponseBody
    @GetMapping(value = "register/office/get")
    public String getAllOffice(){
        return patientService.getAllOffice();
    }

    @ResponseBody
    @GetMapping(value = "register/select/newpatient")
    public String getNewPatient (){
     return    patientService.getPatientById( patientService.getNewPatientId());
    }

    @ResponseBody
    @RequestMapping(value = "diagnosis/user/get",method = RequestMethod.POST)
    public String getAllUser(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        String conven=jsonObject.getString("patConven");
        return patientService.getUserByOfcId(patientService.getOfficeIdByConven(conven));
    }

//    @ResponseBody
//    @RequestMapping(value = "register/select/page",method = RequestMethod.POST)
//    public String page(@RequestBody String data,HttpSession session){
//        JSONObject jsonObject=JSON.parseObject(data);
//        int currPage=jsonObject.getInteger("currPage");
//        int limit=jsonObject.getInteger("limit");
//        return patientService.getPatientByOfficeId(patientService.getOfcIdBySession(session),currPage,limit);
//    }


@ResponseBody
@RequestMapping(value = "update/patient",method = RequestMethod.POST)
public int updatePatient(@RequestBody String data){
    Patient patient=JSONObject.parseObject(data,Patient.class);
//    JSONObject jsonObject=JSON.parseObject(data);
//    int id=jsonObject.getInteger("id");
//    String name=jsonObject.getString("patName");
//    String sex=jsonObject.getString("patSex");
//    String remark=jsonObject.getString("patRemark");
//    int age=jsonObject.getInteger("patAge");
//    String contact=jsonObject.getString("patContact");

    return patientService.update(patient.getId(),patient);
}

    @ResponseBody
    @RequestMapping(value = "delete/patient",method = RequestMethod.POST)
    public int deletePatient(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        int id=jsonObject.getInteger("id");

        return patientService.deleteOneInfo(id);
    }
}
